package com.sz.biz.common.customer.service.impl;

import com.sz.biz.common.customer.entity.CusUserRole;
import com.sz.biz.common.customer.service.CustomerRoleService;
import com.sz.common.base.dao.DaoSupport;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.common.core.service.PrincipalUtils;
import com.sz.common.core.service.role.MembershipType;
import com.sz.common.core.service.role.RoleMembership;
import com.sz.common.core.constants.SysErrorCodes;
import com.sz.common.core.system.entity.Role;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Package: com.sz.biz.common.customer.service.impl <br>
 * Function: TODO: ADD FUNCTION <br>
 * Author: Charles. <br>
 * Date: 2017-05-04 14:07:00.
 */
@Service("CustomerRoleService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class CustomerRoleServiceImpl extends AbstractService implements CustomerRoleService {

    @Resource(name = "daoSupport")
    private DaoSupport dao;

    @Override
    public String getMembershipTypeName() {
        return "CUSTOMER";
    }

    @Override
    public boolean isMembershipAcceptable(RoleMembership membership) {
        return membership != null && MembershipType.CUSTOMER.equals(membership.getMembershipType());
    }

    @Override
    public void assignRoleMembers(int roleId, List<RoleMembership> members) {
        dao.delete("CusUserRoleMapper.deleteByRoleId", roleId);

        if (members == null || members.size() == 0) {
            return;
        }

        List<CusUserRole> addedCusUserRole = new ArrayList<>();

        for (RoleMembership member : members) {
            CusUserRole cur = new CusUserRole();
            cur.setUserId(member.getIdAsInteger());
            cur.setRoleId(roleId);
            addedCusUserRole.add(cur);
        }

        if (addedCusUserRole.size() == 0) {
            return;
        }

        dao.batchInsert("CusUserRoleMapper.insert", addedCusUserRole);

    }

    @Override
    public void removeRoleMembers(int roleId, List<RoleMembership> members) {
        if (members == null || members.size() == 0) {
            return;
        }

        List<Integer> memberIds = new ArrayList<>();
        members.forEach(member -> {
            if (member.getIdAsInteger() > 0) {
                memberIds.add(member.getIdAsInteger());
            } else {
                throw Exceptions.bizException(SysErrorCodes.ERROR_ARGUMENT_EMPTY, "null cusUserId");
            }
        });
        Map<String, Object> param = new HashMap<>();
        param.put("roleId", roleId);
        param.put("list", memberIds);
        dao.delete("CusUserRoleMapper.deleteByRoleIdAndUserIds", param);
    }

    @Override
    public List<Role> getRolesByMember(RoleMembership member) {
        if (!isMembershipAcceptable(member)) {
            return null;
        }

        int domainId = PrincipalUtils.getDomainId();
        ParamData pd = new ParamData();
        pd.put("userId", member.getIdAsInteger());
        pd.put("domainId", domainId);

        List<Role> rolesByCusUser = dao.findForList("CusUserRoleMapper.findRoleByUserId", pd, Role.class);
        return rolesByCusUser;
    }

    @Override
    protected String getMapperNamespace() {
        return "CusUserRoleMapper";
    }
}
